

package com.lry.rpc.starter.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

public abstract class BeanFactoryUtils {
    public BeanFactoryUtils() {
    }

    public static <T> T getOptionalBean(ListableBeanFactory beanFactory, String beanName, Class<T> beanType) {
        if (!StringUtils.hasText(beanName)) {
            return null;
        } else {
            String[] beanNames = (String[])ObjectUtils.of(new String[]{beanName});
            List<T> beans = getBeans(beanFactory, beanNames, beanType);
            return CollectionUtils.isEmpty(beans) ? null : beans.get(0);
        }
    }

    public static <T> List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType) {
        if (org.springframework.util.ObjectUtils.isEmpty(beanNames)) {
            return Collections.emptyList();
        } else {
            String[] allBeanNames = org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, beanType, true, false);
            List<T> beans = new ArrayList(beanNames.length);
            String[] var5 = beanNames;
            int var6 = beanNames.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                String beanName = var5[var7];
                if (org.springframework.util.ObjectUtils.containsElement(allBeanNames, beanName)) {
                    beans.add(beanFactory.getBean(beanName, beanType));
                }
            }

            return Collections.unmodifiableList(beans);
        }
    }
}
